
package com.scs.application.modules.upms.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Lists;
import com.scs.application.core.global.GlobalParamService;
import com.scs.application.core.service.BaseServiceImpl;
import com.scs.application.core.utils.StringUtils;
import com.scs.application.modules.upms.entity.Parameter;
import com.scs.application.modules.upms.mapper.ParameterMapper;
import com.scs.application.modules.upms.request.ParameterQueryRequest;
import com.scs.application.modules.upms.service.ParameterService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 参数配置 service 实现
 *
 */
@Slf4j
@Service
@AllArgsConstructor
public class ParameterServiceImpl extends BaseServiceImpl<ParameterMapper, Parameter> implements ParameterService, InitializingBean {

    private final String DEFAULT_OWNER = "0";
    private final GlobalParamService globalParamService;

    @Override
    public List<Parameter> list(ParameterQueryRequest request) {
        List<String> excludeFields = Lists.newArrayList("creator", "modifier");
        List<Parameter> list = this.list(Wrappers.<Parameter>query()
                .select(Parameter.class, o -> !excludeFields.contains(o.getProperty()))
                .like(StringUtils.isNotBlank(request.getParamName()), "param_name", request.getParamName())
                .like(StringUtils.isNotBlank(request.getParamKey()), "param_key", request.getParamKey())
                .eq(StringUtils.isNotBlank(request.getIsSys()), "is_sys", request.getIsSys())
                .orderByDesc("gmt_modified")
        );
        return list;
    }

    @Override
    public IPage<Parameter> page(ParameterQueryRequest request) {
        IPage page = this.page(request.getPage(), Wrappers.<Parameter>query()

                .like(StringUtils.isNotBlank(request.getParamName()), "param_name", request.getParamName())
                .like(StringUtils.isNotBlank(request.getParamKey()), "param_key", request.getParamKey())
                .eq(StringUtils.isNotBlank(request.getIsSys()), "is_sys", request.getIsSys())
                .nested(o -> o.eq("owner", DEFAULT_OWNER))
                .orderByDesc("param_key")
        );
        return page;
    }

    @Override
    public Parameter get(String owner, String key) {
        return this.getOne(Wrappers.<Parameter>query().eq("owner", owner).eq("param_key", key));
    }

    @Override
    public void syncToCache() {
        // 重新加载
        List<Parameter> list = this.list(Wrappers.<Parameter>query().eq("owner", DEFAULT_OWNER));
        for (Parameter parameter : list) {
            globalParamService.putGlobal(parameter.getParamKey(), parameter.getParamValue());
        }
        log.info(StringUtils.repeat(">", 10) + " load global properties succeed,{} of them are loaded. ", list.size());
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        syncToCache();
    }

}
